// import request from '../../utils/request';
import {isEmptyValue} from '../../utils/publicFunction';
import request from "../../utils/request";
const state = {
    _this: [],//vue实例,可用于调用vue方法，原vue实例内的this方法
    url: '',
    method: 'post',
    loading: false,
    tableData: [],
    pageSize: 20,
    pageNo: 1,
    searchForm: {},
    pageSizes: [20, 50, 100],
    total: 0,
    templates:[],
    templates_detail:{},
};

const getters = {
};

const mutations = {
//页面初始化
    pageInit(state, args) {
        state._this = args;
        state.url = args.url;
        if (args.method) {
            state.method = args.method
        }
        state.searchForm = args.searchForm;
        state.pageNo = 1;
        state.total = 0;
    },
    //刷新表单处理
    tableReload(state) {
        state.pageNo = 1;
        state.total = 0;
    },
    //重置搜索条件，刷新表单处理
    tableReset(state) {
        state.pageNo = 1;
        state.total = 0;
        state._this.$refs['searchForm'].resetFields();
    },
};

const actions = {
//页面初始化
    pageInit({commit, dispatch, rootState}, args) {
        this.commit('pageInit', args);
        this.dispatch('loadTable');
    },
    //刷新表单
    tableReload() {
        this.commit('tableReload');
        this.dispatch('loadTable');
    },
    //重置搜索条件，刷新表单
    tableReset() {
        this.commit('tableReset');
        this.dispatch('loadTable');
    },
    //加载table
    loadTable() {
        state.loading = true;
        //清除为空的参数
        let searchForm = state.searchForm;
        let params = {};
        for (let i in searchForm) {
            if (searchForm[i] === 0 ||(searchForm[i] && searchForm[i] !== [])) {
                params[i] = state._this.deepCopy(searchForm[i]);
            }
        }
        //拼接页数到搜索条件内
        params.pageSize = state.pageSize;
        params.pageNo = state.pageNo;

        (state.method === "get" ? state._this.get : state._this.post)(state.url, params).then(result => {
            if (result.code === 0) {
                state.tableData = result.data.info;
                state.total = result.data.total;
            } else {
                state._this.$message.error(result.msg);
            }
            state.loading = false;
        }).catch(e => {
            state.loading = false;
            state._this.$message.error('数据加载异常');
            console.log(e);
        });
    },
    templates(){
        if(isEmptyValue(state.templates)){
            request.post('/api/setting/template/data').then(result => {
                if (result.code === 0) {
                    state.templates = result.data.templates
                    state.templates_detail = result.data.templates_detail
                } else {
                    state._this.$message.error('数据加载异常');
                }
            }).catch(e => {
                state._this.$message.error('数据加载异常');
                console.log(e);
            })
        }
    },
};

export default{
    namespaced: false,
    state,
    getters,
    mutations,
    actions,
}
